// pages/article/lanmu/lanmu.js
const app = getApp()
const util = require("../../../utils/util.js");
var page = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        lanmuHeight: 0,
        classlist: [], // 栏目文章列表
        lanmuLsit: [], // 子栏目
        bannerlist: [], // 轮播图列表

        showLoading: false,
        shownull: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            id: options.id ? options.id : '',
            type: options.type ? options.type : '',
            citycode: options.city ? options.city : (ks.getStorageSync("citycode") ? ks.getStorageSync("citycode") : 'sz'),
            // name: options.name ? decodeURIComponent(options.name):""
        })

        util.getUrl(app.globalData.url).then((res) => {
            util.cityList().then((res) => {
                let citydata = res.allcity.filter(v => v.citycode == this.data.citycode);
                this.setData({
                    city: citydata[0].cityname
                })
                this.setData({
                    showLoading: true,
                })
                this.getclassList();
                this.getclassInfo(this.data.id, page);
                this.getlanmuList(this.data.id);
            })
        })

    },

    // 获取单个栏目信息
    getclassList() {
        let data = {
            citycode: this.data.type == 'wenda' ? 'wenda' : ((this.data.type == 'bsy' && this.data.citycode == 'sz') ? 'bsy' : this.data.citycode),
            action: 'classinfo',
            classid: this.data.id,
        };
        util.request('index.php', data, 'GET').then(res => {
            this.setData({
                    classname: res.data.classname,
                })
                // this.classname = res.data.classname;
            ks.setNavigationBarTitle({
                title: res.data.classname,
            })
        })
    },

    // 获取单一栏目详情列表接口
    getclassInfo(classid, page) {
        let infoData = {
            citycode: this.data.type == 'wenda' ? 'wenda' : ((this.data.type == 'bsy' && this.data.citycode == 'sz') ? 'bsy' :
                this.data.citycode),
            action: 'list',
            city: this.data.city,
            pagesize: 15,
            classid,
            page,
            // timestamp: new Date().getTime(),
        };
        util.request('index.php', infoData, 'GET').then(res => {
            // if (res.data == "") {
            //   this.requestStatus = "noMore";
            // }
            this.setData({
                classlist: this.data.classlist.concat(res.data),
            })
            this.setData({
                showLoading: false,
            })

            if (this.data.classlist == "") {
                this.setData({
                    shownull: true,
                })
            }
            // this.classlist = this.classlist.concat(res.data);
            // if (this.classlist.length < 15) {
            //   this.requestStatus = "noMore";
            // }

        })
    },

    // 获取栏目列表
    getlanmuList(classid) {
        let lanmuData = {
            action: "classlist",
            citycode: this.data.type == 'wenda' ? 'wenda' : ((this.data.type == 'bsy' && this.data.citycode == 'sz') ? 'bsy' :
                this.data.citycode),
            // siteid:162
            classid,
        };
        util.request('index.php', lanmuData, 'GET').then(res => {
            if (res.data) {
                res.data.forEach((v, k) => {
                    v.checked = false;
                })
                this.setData({
                    lanmuLsit: res.data,
                })

                var query = ks.createSelectorQuery()
                query.select('.lanmus').boundingClientRect((res) => {
                    this.setData({
                        lanmuHeight: (res != null) ? res.height : 0,
                    })
                }).exec()
            }
        })
    },

    // 栏目点击
    lanmuClick(e) {
        // this.lanmuIndex = index;
        let id = e.currentTarget.dataset.id;
        let classname = e.currentTarget.dataset.classname;
        let index = e.currentTarget.dataset.index;
        this.data.lanmuLsit.forEach((v, k) => {
            if (index == k) {
                v.checked = true;
            } else {
                v.checked = false;
            }
        })

        this.setData({
            lanmuLsit: this.data.lanmuLsit,
            classlist: [],
            classid: id,
            id,
            classname: classname,
            showLoading: true,
            shownull: false,
        })
        this.getclassInfo(id, 1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        page++;
        // this.requestStatus = "loading";
        this.getclassInfo(this.data.id, page);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})